package org.foreveross.taluo.infra;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;

import org.foreveross.que.infra.base.ddd.repository.EntityHibernateRepository;
import org.foreveross.taluo.entity.AcRoleEntity;
import org.foreveross.taluo.entity.AcRolegroupEntity;
import org.foreveross.taluo.entity.AcUserEntity;
import org.foreveross.taluo.entity.AcUsergroupEntity;


public class TaluoEntityRepository extends EntityHibernateRepository implements ITaluoBaseRepository  {
	
	
	public List<AcUserEntity> findAcUsers(){
		List<AcUserEntity> users =  super.queryResult("from AcUserEntity t where t.valid=1");
		return users;
	}
	
	@SuppressWarnings("unused")
	public Map <Long,List<AcRolegroupEntity>> findAcUserRoleR(List <AcUserEntity> users){
		List  <AcUsergroupEntity> usergroups = super.queryResult("from AcUsergroupEntity t");
		List rolegroups = super.queryResult("from AcRolegroupEntity t");
		if(users==null){
			return null;
		}
		Map <Long,List<AcRolegroupEntity>> result = new HashMap<Long, List<AcRolegroupEntity>>();
		for(AcUserEntity user : users){
			List<AcUsergroupEntity> userGroups = this.queryResult("from AcUsergroupEntity t where t.id.userids="+user.getIds());
			if(userGroups==null){
				continue;
			}else{
				for(AcUsergroupEntity usergroup:userGroups){
					List<AcRolegroupEntity> roleGroups = this.queryResult("from AcRolegroupEntity t where t.id.groupids="+usergroup.getId().getGroupids());
					result.put(user.getIds(), roleGroups);
				}
			}
		}
		return result;
	}
	
	public List<AcRoleEntity> findAcRoles(){
		return super.queryResult("from AcRoleEntity t where t.valid = 1");
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		super.entityManager = entityManager;
	}
	
}
